package cn.sczhyt.modules.sys.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.sczhyt.common.annotation.SysLog;
import cn.sczhyt.common.validator.ValidatorUtils;
import cn.sczhyt.modules.sys.dao.SysUserDao;
import cn.sczhyt.modules.sys.entity.SysUserEntity;
import cn.sczhyt.modules.sys.form.OrganizeInfoForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.sczhyt.modules.sys.entity.SysOrganizeInfoEntity;
import cn.sczhyt.modules.sys.service.SysOrganizeInfoService;
import cn.sczhyt.common.utils.R;



/**
 * 组织机构信息表
 *
 * @author luojialin
 * @email 78357966@qq.com
 * @date 2020-06-02 17:40:27
 */
@Api(tags = "组织机构")
@RestController
@RequestMapping("sys/orginfo")
public class SysOrganizeInfoController extends AbstractController {

    @Autowired
    private SysOrganizeInfoService sysOrganizeInfoService;
    @Autowired
    private SysUserDao userDao;
    /**
     * 组织机构列表
     */
    @ApiOperation("组织机构列表")
    @GetMapping("/list")
    //@RequiresPermissions("sys:orginfo:list")
    public R list(@RequestParam Map<String, Object> params){
        List<SysOrganizeInfoEntity> all = sysOrganizeInfoService.findAll();

        return R.ok().put("data", all);
    }
    /**
     * 承办单位列表
     */
    @ApiOperation("承办单位列表")
    @GetMapping("/listdw")
    //@RequiresPermissions("sys:orginfo:listdw")
    public R list(){
        List<SysOrganizeInfoEntity> all = sysOrganizeInfoService.findAllDw();

        return R.ok().put("data", all);
    }

//    /**
//     * 组织机构详情信息
//     */
//    @RequestMapping("/info/{organizeId}")
//    @RequiresPermissions("sys:orginfo:info")
//    public R info(@PathVariable("organizeId") String organizeId){
//        //查询单位的信息
//        SysOrganizeInfoEntity sysOrganizeInfo = sysOrganizeInfoService.getById(organizeId);
//        String userId = sysOrganizeInfo.getUserId();
//        SysUserEntity userEntity = userDao.selectById(userId);
//        return R.ok().put("data", sysOrganizeInfo).put("user",userEntity);
//    }

    /**
     * 组织机构详情信息
     */
    @ApiOperation("组织机构详情信息")
    @GetMapping("/info/{userId}")
    public R OrgInfo(@PathVariable("userId") String userId){

        //查询单位的信息
        SysOrganizeInfoEntity sysOrganizeInfo = sysOrganizeInfoService.getByUserId(userId);

        return R.ok().put("data", sysOrganizeInfo);
    }

    /**
     * 新增单位账号,并分配对应角色
     */
    @ApiOperation("新增单位账号,并分配对应角色")
    @SysLog("新增组织机构账号")
    @PostMapping("/save/{roleId}")
    @RequiresPermissions("sys:orginfo:save")
    public R save(@RequestBody OrganizeInfoForm vo ,
                  @PathVariable("roleId") String roleId){

        sysOrganizeInfoService.AddOrg(vo,roleId);

        return R.ok().put("data",vo);
    }

    /**
     * 修改
     */
    @ApiOperation("修改组织机构账号")
    @SysLog("修改组织机构账号")
    @PostMapping("/update")
    // @RequiresPermissions("sys:orginfo:update")
    public R update(@RequestBody SysOrganizeInfoEntity sysOrganizeInfo){
        ValidatorUtils.validateEntity(sysOrganizeInfo);
        sysOrganizeInfo.getOrganizeName();
        sysOrganizeInfo.getEmail();
        sysOrganizeInfo.getMobile();
        sysOrganizeInfoService.updateById(sysOrganizeInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除组织机构账号")
    @SysLog("删除组织机构账号")
    @PostMapping("/delete")
    @RequiresPermissions("sys:orginfo:delete")
    public R delete(@RequestBody String[] ids){
        //通过organizeIds查询对应的userIds
        List<SysOrganizeInfoEntity> list = sysOrganizeInfoService.findByIds(ids);

        sysOrganizeInfoService.removeByIds(Arrays.asList(ids));
        //获取需要删除的 userIds
        ArrayList<String> userIds = new ArrayList<>();
        for(SysOrganizeInfoEntity organizeInfoEntity : list){
            String userId = organizeInfoEntity.getUserId();
            if(userId != null){
                userIds.add(userId);
            }
        }
        //删除对应的userIds
        userDao.deleteBatchIds(userIds);
        return R.ok();
    }

}
